(function(){
	global  = window;
	global.module = null;
	
	var services = {};
	services['console'] = console;
	
	namespace = function(name, context){
		var values = name.split('.',2);
		if( !context ){
			context = global;
		}
		if( !context[values[0]] ){
			context[values[0]] = {}
		}
		context = context[values[0]];
		if( values.length == 2){
			namespace(name.substring(name.indexOf('.')+1), context);
		}
	};
	
	var lookup = function(name){
		var segments = name.split('.');
		var ctx = window;
		for(var i=0; i < segments.length; i++){
			if( ctx && ctx[segments[i]] ) ctx = ctx[segments[i]];
			else ctx = null;
		}
		return ctx;
	};
	
	var clean = function(name){
		if( name.match(/.*[.]js$/) ) name = name.substring(0, name.lastIndexOf('.'))
		if( name.match(/^[.]\/.*/) ) name = name.substring(name.indexOf('/')+1);
		return name;
	}
	var isNullOrEmpty = function(obj){
		if( !obj ) return true;
		for(member in obj)
			return false;
		return true;
	};
	require = function(name){
		name = clean(name);
		var service = services[name];
		var exports = services[name+".exports"];
		if(!service){
			var ctor = lookup(name);
			if(exports && !isNullOrEmpty(exports.module.exports)){
				return exports.module.exports;
			}else if( ctor ){
				services[name] = new ctor();
			}else{
				services[name+'.exports'] = {
					module : {
						exports : {}
					}
				};
				console.error('Servie not found, loading default : ' + name);
				return services[name+".exports"].module.exports;
				//throw 'Service not found : ' + name;
			}
		}
		return services[name];
	};
	services['modules'] = {
		add : function(name, service){
			services[name] = service;
		}	
	};
	init = function(name){
		name = clean(name);
		if(!services[name+'.exports']){
			module  = {
				exports : {}
			};
			services[name + '.exports'] = {
				module : module
			};
			exports = module.exports;
		}else{
			module  = services[name+'.exports'].module;
			exports = module.exports;
		}
	};
	
	namespace('org.jsmud');
})()